var _n17;
var cc__extends = __extends;
var cc__decorate = __decorate;
var cc__importDefault = __importDefault;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var s;
var c;
var $1$Ipha_tool = require("Ipha_tool");
var l = cc__importDefault($1$Ipha_tool);
var $1$Ipha_ComponentBasic = require("Ipha_ComponentBasic");
var u = cc__importDefault($1$Ipha_ComponentBasic);
var cc__decorator = cc._decorator;
var ccp_ccclass = cc__decorator.ccclass;
var ccp_property = cc__decorator.property;
var ccp_menu = cc__decorator.menu;
var ccp_executeInEditMode = cc__decorator.executeInEditMode;
(function (t) {
  t[t.left = 0] = "left";
  t[t.right = 1] = "right";
  t[t.top = 2] = "top";
  t[t.bottom = 3] = "bottom";
  t[t.horizontalCenter = 4] = "horizontalCenter";
  t[t.verticalCenter = 5] = "verticalCenter";
})(s || (s = {}));
(function (t) {
  t[t.none = 0] = "none";
  t[t.alignment = 1] = "alignment";
  t[t.distance = 2] = "distance";
})(c || (c = {}));
var def_Ipha_Correlation = function (t) {
  function _ctor() {
    // 创建实例并设置默认属性
    const instance = null !== t && t.apply(this, arguments) || this;

    // 初始化实例属性
    instance.comName = "节点关联对齐组件"; // 组件名称
    instance.ndTarget = null; // 目标节点
    instance.type = c.none; // 对齐类型

    // 初始化边界和偏移量属性
    instance.left = false;
    instance.leftOffset = 0;
    instance.right = false;
    instance.rightOffset = 0;
    instance.top = false;
    instance.topOffset = 0;
    instance.bottom = false;
    instance.bottomOffset = 0;

    // 初始化中心对齐和偏移量属性
    instance.horizontalCenter = false;
    instance.horizontalCenterOffset = 0;
    instance.verticalCenter = false;
    instance.verticalCenterOffset = 0;

    // 初始化相对距离的属性
    instance.leftD = false;
    instance.leftDOffset = 0;
    instance.rightD = false;
    instance.rightDOffset = 0;
    instance.topD = false;
    instance.topDOffset = 0;
    instance.bottomD = false;
    instance.bottomDOffset = 0;

    // 初始化对齐状态
    instance._isRun = true;
    instance._isAlignment = true;
    instance._isDistance = true;

    return instance; // 返回实例
  }
  cc__extends(_ctor, t);
  Object.defineProperty(_ctor.prototype, "isRun", {
    get() {
      return this._isRun;
    },
    set(t) {
      this._isRun = t;
    },
    enumerable: false,
    configurable: true
  });
  Object.defineProperty(_ctor.prototype, "isAlignment", {
    get() {
      return this._isAlignment;
    },
    set(t) {
      this._isAlignment = t;
    },
    enumerable: false,
    configurable: true
  });
  Object.defineProperty(_ctor.prototype, "isDistance", {
    get() {
      return this._isDistance;
    },
    set(t) {
      this._isDistance = t;
    },
    enumerable: false,
    configurable: true
  });
  _ctor.prototype.setTarget = function (t) {
    this.ndTarget = t;
  };
  _ctor.prototype.onLoad = function () {
    let t; // 声明变量

    // 如果 ndTarget 为空，则将其设置为当前节点的父节点
    if (this.ndTarget === null) {
      const node = this.node; // 获取当前节点
      this.ndTarget = (node === null || node === undefined) ? undefined : node.parent; // 设置 ndTarget
    }
  };
  _ctor.prototype.start = function () {
    // 检查是否可以运行并且目标节点存在
    if (this._isRun && this.ndTarget) {
      // 如果目标节点在层级中激活
      if (this.ndTarget.activeInHierarchy) {
        this.alignment(); // 对齐操作
        this.distance(); // 距离调整
      } else {
        this.node.setPosition(cc.v2(0, 0)); // 如果目标节点不激活，则重置当前位置
      }
    }
  };
  _ctor.prototype.alignment = function () {
    // 检查对齐类型和对齐状态
    if (this.type === c.alignment && this._isAlignment) {
      // 获取目标在当前父节点空间的位置
      const targetPosition = l.default.pos.rootPosToTargetNodeSpace(this.ndTarget, this.node.parent);

      // 处理水平对齐
      if (this.left && this.right) {
        const leftEdge = targetPosition.x - this.ndTarget.width * this.ndTarget.scaleX * this.ndTarget.anchorX + this.leftOffset;
        const rightEdge = targetPosition.x + this.ndTarget.width * this.ndTarget.scaleX * (1 - this.ndTarget.anchorX) - this.rightOffset;
        this.node.width = Math.abs(rightEdge - leftEdge); // 设置节点宽度
        this.node.x = leftEdge + this.node.width * this.ndTarget.scaleX * this.node.anchorX; // 设置节点位置
      } else if (this.left) {
        const leftEdge = targetPosition.x - this.ndTarget.width * this.ndTarget.scaleX * this.ndTarget.anchorX;
        this.node.x = leftEdge + this.node.width * this.ndTarget.scaleX * this.node.anchorX + this.leftOffset;
      } else if (this.right) {
        const rightEdge = targetPosition.x + this.ndTarget.width * this.ndTarget.scaleX * (1 - this.ndTarget.anchorX);
        this.node.x = rightEdge - this.node.width * this.ndTarget.scaleX * (1 - this.node.anchorX) - this.rightOffset;
      } else if (this.verticalCenter) {
        const centerPos = targetPosition.x - this.ndTarget.width * this.ndTarget.scaleX * this.ndTarget.anchorX;
        this.node.x = centerPos + this.ndTarget.width * this.ndTarget.scaleX * 0.5 - this.node.width * this.ndTarget.scaleX * (0.5 - this.node.anchorX) + this.verticalCenterOffset;
      }

      // 处理垂直对齐
      if (this.top && this.bottom) {
        const topEdge = targetPosition.y + this.ndTarget.height * this.ndTarget.scaleY * (1 - this.ndTarget.anchorY) - this.topOffset;
        const bottomEdge = targetPosition.y - this.ndTarget.height * this.ndTarget.scaleY * this.ndTarget.anchorY + this.bottomOffset;
        this.node.height = topEdge - bottomEdge; // 设置节点高度
        this.node.y = bottomEdge + this.node.height * this.ndTarget.scaleY * this.node.anchorY; // 设置节点位置
      } else if (this.top) {
        const topEdge = targetPosition.y + this.ndTarget.height * this.ndTarget.scaleY * (1 - this.ndTarget.anchorY) - this.topOffset;
        this.node.y = topEdge - this.node.height * this.ndTarget.scaleY * (1 - this.node.anchorY);
      } else if (this.bottom) {
        const bottomEdge = targetPosition.y - this.ndTarget.height * this.ndTarget.scaleY * this.ndTarget.anchorY + this.bottomOffset;
        this.node.y = bottomEdge + this.node.height * this.ndTarget.scaleY * this.node.anchorY;
      } else if (this.horizontalCenter) {
        const centerPos = targetPosition.y - this.ndTarget.height * this.ndTarget.scaleY * this.ndTarget.anchorY;
        this.node.y = centerPos + this.ndTarget.height * this.ndTarget.scaleY * 0.5 - this.node.height * this.ndTarget.scaleY * (0.5 - this.node.anchorY) + this.horizontalCenterOffset;
      }
    }
  };
  _ctor.prototype.distance = function () {
    // 检查距离对齐类型及其状态
    if (this.type === c.distance && this._isDistance) {
      // 获取目标在当前父节点空间中的位置
      const targetPosition = l.default.pos.rootPosToTargetNodeSpace(this.ndTarget, this.node.parent);

      // 处理水平距离对齐
      if (this.leftD) {
        const leftEdge = targetPosition.x - this.ndTarget.width * this.ndTarget.scaleX * this.ndTarget.anchorX;
        this.node.x = leftEdge - (this.node.width * this.ndTarget.scaleX * (1 - this.node.anchorX) + this.leftDOffset);
      } else if (this.rightD) {
        const rightEdge = targetPosition.x + this.ndTarget.width * this.ndTarget.scaleX * (1 - this.ndTarget.anchorX);
        this.node.x = rightEdge + (this.node.width * this.ndTarget.scaleX * this.node.anchorX + this.rightDOffset);
      }

      // 处理垂直距离对齐
      if (this.topD) {
        const topEdge = targetPosition.y + this.ndTarget.height * this.ndTarget.scaleY * (1 - this.ndTarget.anchorY);
        this.node.y = topEdge + (this.node.height * this.ndTarget.scaleY * this.node.anchorY + this.topDOffset);
      } else if (this.bottomD) {
        const bottomEdge = targetPosition.y - this.ndTarget.height * this.ndTarget.scaleY * this.ndTarget.anchorY;
        this.node.y = bottomEdge - (this.node.height * this.ndTarget.scaleY * (1 - this.node.anchorY) + this.bottomDOffset);
      }
    }
  };
  _ctor.prototype._RunTheCorrelation = function () {
    // 检查目标节点是否存在
    if (this.ndTarget) {
      // 如果目标节点在层级中激活
      if (this.ndTarget.activeInHierarchy) {
        this.alignment(); // 调用对齐方法
        this.distance();   // 调用距离调整方法
      } else {
        this.node.setPosition(cc.v2(0, 0)); // 如果目标节点不激活，则将当前节点位置重置为 (0, 0)
      }
    }
  };
  _ctor.prototype.update = function () {
    this._isRun && this._RunTheCorrelation();
  };
  cc__decorate([ccp_property({
    type: cc.Node,
    displayName: "目标节点"
  })], _ctor.prototype, "ndTarget", undefined);
  cc__decorate([ccp_property({
    type: cc.Enum(c),
    displayName: "使用模式",
    tooltip: "空\n对齐\n间隔"
  })], _ctor.prototype, "type", undefined);
  cc__decorate([ccp_property({
    displayName: "左对齐",
    visible() {
      return this.type == c.alignment && !this.verticalCenter;
    }
  })], _ctor.prototype, "left", undefined);
  cc__decorate([ccp_property({
    displayName: "左对齐偏差",
    visible() {
      return this.type == c.alignment && this.left;
    }
  })], _ctor.prototype, "leftOffset", undefined);
  cc__decorate([ccp_property({
    displayName: "右对齐",
    visible() {
      return this.type == c.alignment && !this.verticalCenter;
    }
  })], _ctor.prototype, "right", undefined);
  cc__decorate([ccp_property({
    displayName: "右对齐偏差",
    visible() {
      return this.type == c.alignment && this.right;
    }
  })], _ctor.prototype, "rightOffset", undefined);
  cc__decorate([ccp_property({
    displayName: "顶对齐",
    visible() {
      return this.type == c.alignment && !this.horizontalCenter;
    }
  })], _ctor.prototype, "top", undefined);
  cc__decorate([ccp_property({
    displayName: "顶对齐偏差",
    visible() {
      return this.type == c.alignment && this.top;
    }
  })], _ctor.prototype, "topOffset", undefined);
  cc__decorate([ccp_property({
    displayName: "底对齐",
    visible() {
      return this.type == c.alignment && !this.horizontalCenter;
    }
  })], _ctor.prototype, "bottom", undefined);
  cc__decorate([ccp_property({
    displayName: "底对齐偏差",
    visible() {
      return this.type == c.alignment && this.bottom;
    }
  })], _ctor.prototype, "bottomOffset", undefined);
  cc__decorate([ccp_property({
    displayName: "水平居中",
    visible() {
      return this.type == c.alignment && !this.top && !this.bottom;
    }
  })], _ctor.prototype, "horizontalCenter", undefined);
  cc__decorate([ccp_property({
    displayName: "水平居中偏差",
    visible() {
      return this.type == c.alignment && this.horizontalCenter;
    }
  })], _ctor.prototype, "horizontalCenterOffset", undefined);
  cc__decorate([ccp_property({
    displayName: "竖直居中",
    visible() {
      return this.type == c.alignment && !this.left && !this.right;
    }
  })], _ctor.prototype, "verticalCenter", undefined);
  cc__decorate([ccp_property({
    displayName: "竖直居中偏差",
    visible() {
      return this.type == c.alignment && this.verticalCenter;
    }
  })], _ctor.prototype, "verticalCenterOffset", undefined);
  cc__decorate([ccp_property({
    displayName: "于左间隔",
    visible() {
      return this.type == c.distance && !this.rightD;
    }
  })], _ctor.prototype, "leftD", undefined);
  cc__decorate([ccp_property({
    displayName: "于左间隔偏差",
    visible() {
      return this.type == c.distance && this.leftD;
    }
  })], _ctor.prototype, "leftDOffset", undefined);
  cc__decorate([ccp_property({
    displayName: "于右间隔",
    visible() {
      return this.type == c.distance && !this.leftD;
    }
  })], _ctor.prototype, "rightD", undefined);
  cc__decorate([ccp_property({
    displayName: "于右间隔偏差",
    visible() {
      return this.type == c.distance && this.rightD;
    }
  })], _ctor.prototype, "rightDOffset", undefined);
  cc__decorate([ccp_property({
    displayName: "于顶间隔",
    visible() {
      return this.type == c.distance && !this.bottomD;
    }
  })], _ctor.prototype, "topD", undefined);
  cc__decorate([ccp_property({
    displayName: "于顶间隔偏差",
    visible() {
      return this.type == c.distance && this.topD;
    }
  })], _ctor.prototype, "topDOffset", undefined);
  cc__decorate([ccp_property({
    displayName: "于底间隔",
    visible() {
      return this.type == c.distance && !this.topD;
    }
  })], _ctor.prototype, "bottomD", undefined);
  cc__decorate([ccp_property({
    displayName: "于底间隔偏差",
    visible() {
      return this.type == c.distance && this.bottomD;
    }
  })], _ctor.prototype, "bottomDOffset", undefined);
  return cc__decorate([ccp_ccclass, ccp_executeInEditMode, ccp_menu("ipha组件/节点关联对齐组件")], _ctor);
}(u.default);
exports.default = def_Ipha_Correlation;